package com.mls.fivedistrict.server.impl;

import com.mls.fivedistrict.entity.request.AwardRequest;
import com.mls.fivedistrict.enums.RecordProductTypeEnum;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsFlash;
import com.mls.fivedistrict.pojo.NewsFlashLikeRecord;
import com.mls.fivedistrict.pojo.NewsFlashShareReport;
import com.mls.fivedistrict.dao.NewsFlashShareReportMapper;
import com.mls.fivedistrict.server.AwardService;
import com.mls.fivedistrict.server.NewsFlashService;
import com.mls.fivedistrict.server.NewsFlashShareReportService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 新闻分享报表 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-06-12
 */
@Service
public class NewsFlashShareReportServiceImpl extends ServiceImpl<NewsFlashShareReportMapper, NewsFlashShareReport> implements NewsFlashShareReportService {
    @Autowired
    AwardService awardService;
    @Autowired
    NewsFlashService newsFlashService;
    @Transactional
    @Override
    public boolean addNewsFlashShareReport(NewsFlashShareReport newsFlashShareReport) {
        boolean flag = insert(newsFlashShareReport);
        if (flag) {
            NewsFlash newsFlash = newsFlashService.selectById(newsFlashShareReport.getNewsFlashId());
            if (newsFlash == null) {
                throw new CustomerException("快讯不存在");
            }
            if (newsFlash.getShareDwqNumber().compareTo(new BigDecimal(0)) > 0) {
                AwardRequest awardRequest = new AwardRequest();
                awardRequest.setTargetUserId(newsFlashShareReport.getUserId());
                awardRequest.setIntegralId(18L);
                awardRequest.setAwardNumber(newsFlash.getShareDwqNumber());
                awardRequest.setSource(RecordProductTypeEnum.CONTRIBUTION.getValue());
                awardRequest.setReason("分享快讯奖励");
                awardService.awardDwq(awardRequest);
            }
        }
        return flag;
    }
}
